# -*- coding: utf-8 -*-
import scopt.test.common
from scopt.mpu.business.settings.handlers import warehouse


class WarehouseCreate(scopt.test.common.ScoptTest):
    ''' Warehouse (WHS) creation tests
    MPU should:

    1. test_warehouse_create_unique ->
        allow to create WHS
    2. test_warehouse_create_duplicate ->
        disallow to create WHS with same name
    3. TODO: create-read WHS test
    '''
    def test_warehouse_create_unique(self):
        # Warehouse in Alaska is not part of demo data
        request = self.request(warehouse.create)
        request['reqd']['name'] = 'Warehouse in Alaska'

        response = self.send_request(request)
        response_status = response['resh']['status']
        self.assertEqual(response_status, 'OK')

    def test_warehouse_create_duplicate(self):
        # Warehouse in New Jersey / Sklad v New Jersey is part of demo data
        request = self.request(warehouse.create)
        request['reqd']['name'] = \
            u'Warehouse in New Jersey / Sklad v New Jersey'

        response = self.send_request(request,
                                     pass_interrupts=True)
        self.assertStatus(response, 'INT', 'business.violation.unique_key')


class WarehouseSelect(scopt.test.common.ScoptTest):
    ''' Warehouse (WHS) select tests
    MPU should:

    1. test_warehouse_select_one ->
        select one WHS by whs_id
    2. test_warehouse_select_all ->
        select all WHSs by not providing id
    3. test_warehouse_select_none ->
        select none WHS by providing id of non existent record
    '''
    def test_warehouse_select_one(self):
        # row with id 1 is part of demo data
        request = self.request(warehouse.select)
        request['reqd']['id'] = 1

        response = self.send_request(request)
        response_status = response['resh']['status']
        data = response['resd']
        self.assertEqual(response_status, 'OK')
        self.assertEqual(len(data), 1)

    def test_warehouse_select_all(self):
        # there are 2 rows in uom demo data
        request = self.request(warehouse.select)
        request['reqd'] = {}
        response = self.send_request(request)
        response_status = response['resh']['status']
        data = response['resd']
        self.assertEqual(response_status, 'OK')
        self.assertEqual(len(data), 2)

    def test_warehouse_select_none(self):
        # row with id of 200 is not present in demo data
        request = self.request(warehouse.select)
        request['reqd']['id'] = 200

        response = self.send_request(request)
        response_status = response['resh']['status']
        data = response['resd']
        self.assertEqual(response_status, 'OK')
        self.assertEqual(len(data), 0)


class WarehouseUpdate(scopt.test.common.ScoptTest):
    ''' Warehouse (WHS) update tests
    MPU should:

    1. test_warehouse_update_new_name ->
        allow update WHS to other unique name
    2. test_warehouse_update_duplicate_name ->
        disallow update WHS to name which is not unique
    3. test_warehouse_update_invalid_id ->
        disallow update WHS with invalid id
    4. test_warehouse_update_missing_id ->
        disallow update WHS with id is missing
    5. TODO: update-read WHS test
    '''
    def test_warehouse_update_new_name(self):
        # row with id 1(kg) is part of demo data
        request = self.request(warehouse.update)
        request['reqd']['id'] = 1
        request['reqd']['name'] = 'kgx'

        response = self.send_request(request)
        response_status = response['resh']['status']
        self.assertEqual(response_status, 'OK')

    def test_warehouse_update_duplicate_name(self):
        # row with name 'Warehouse in Banská Bystrica / Sklad v Banskej
        # Bystrici' is part of demo data
        request = self.request(warehouse.update)
        request['reqd']['id'] = 1
        request['reqd']['name'] = \
        u'Warehouse in Banská Bystrica / Sklad v Banskej Bystrici'

        response = self.send_request(request,
                                     pass_interrupts=True)
        self.assertStatus(response, 'INT', 'business.violation.unique_key')

    def test_warehouse_update_invalid_id(self):
        # rows with id over 100 are not part of demo data
        request = self.request(warehouse.update)
        request['reqd']['id'] = 101
        request['reqd']['name'] = 'm'

        response = self.send_request(request,
                                     pass_interrupts=True)
        self.assertStatus(response, 'INT', 'business.record_no.is_invalid')

    def test_warehouse_update_missing_id(self):
        request = self.request(warehouse.update)
        request['reqd']['name'] = 'm'

        response = self.send_request(request,
                                     pass_interrupts=True)
        self.assertStatus(response, 'INT', 'business.record_no.is_none')
